import { defineStore } from "pinia";
import {ref} from "vue";
import {UserLoginInfo} from "../type/user";
import {getUser} from "../service/user.ts";
import ACCESS_ENUM from "../access/accessEnum.ts";


export const userStore = defineStore("loginUser", () => {
  const loginUser = ref<UserLoginInfo | undefined>({
    id:0,
    userName:"",
    userPhone:"",
    userAvatar:"",
    userRole:"",
    deliveryAddressId:0
  })
  const updateLoginUser = async () => {
    // 从远程请求获取登录信息
    const res = await getUser();
    if (res.code === 0) {
      // @ts-ignore
      loginUser.value = res.data;
    } else {
      //合并两个对象
      // @ts-ignore
      loginUser.value = {...loginUser.value,userRole: ACCESS_ENUM.NOT_LOGIN };
    }
  };

  /**
   * 设置用户地址id
   * @param id
   * */
  const setDeliveryAddressId = (id:number) => {
    // @ts-ignore
    loginUser.value.deliveryAddressId = id;
  }

  return { loginUser, updateLoginUser,setDeliveryAddressId };
});
